/*
 * Copyright (c) 2013 by Chances.
 * $CVSHeader$
 * $Author$
 * $Date$
 * $Revision$
 */
package chances.epg.navigator.template.loader;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;

import chances.epg.navigator.template.TemplateConfig;
import chances.epg.navigator.template.TemplateParameter;

/**
 * Class comments.
 * 
 * @author <a href="mailto:libf@chances.com.cn">libf</a>
 */


public class XMLTemplateConfigLoader implements TemplateConfigLoader {
	private String[] configs;// 配置文件

	private Set<TemplateConfig> templateConfigs;



	/**
	 * 装载模板绑定配置
	 * 
	 * @throws SAXException
	 * @throws IOException
	 */
	public Collection<TemplateConfig> loadTemplateConfig() {
		this.getTemplateConfigs().clear();
		for (int i = 0; i < configs.length; i++) {
			File configFile = new File(getClass().getResource("/").getFile().toString() + "/.." + configs[i]);
			this.parse(configFile);
		}

		// 将模板配置加入到仓库中
		return this.getTemplateConfigs();
	}

	public void parse(File xml) {
		Digester digester = new Digester();
		digester.push(this);
		this.setRule(digester);

		try {
			digester.parse(xml);
		} catch (IOException ex) {
			ex.printStackTrace();
		} catch (SAXException ex) {
			ex.printStackTrace();
		} finally {
			digester.clear();
		}
	}

	private void setRule(Digester digester) {
		digester.addObjectCreate("bind-config/bind", TemplateConfig.class);
		digester.addSetProperties("bind-config/bind", "bizCode", "bizCode");
		digester.addSetProperties("bind-config/bind", "templateType", "templateType");
		digester.addSetProperties("bind-config/bind", "templatePackageCode", "templatePackageCode");
		digester.addSetProperties("bind-config/bind", "contentType", "contentType");
		digester.addSetProperties("bind-config/bind", "templatePath", "templatePath");

		digester.addObjectCreate("bind-config/bind/params/param", TemplateParameter.class);
		digester.addSetProperties("bind-config/bind/params/param", "name", "name");
		digester.addSetProperties("bind-config/bind/params/param", "type", "type");
		digester.addSetProperties("bind-config/bind/params/param", "value", "value");
		digester.addSetNext("bind-config/bind/params/param", "addTemplateParameter");

		digester.addSetNext("bind-config/bind", "addTemplateConfig");
	}

	/**
	 * 添加模板配置.
	 * 
	 * @param templateConfig
	 */
	public void addTemplateConfig(TemplateConfig templateConfig) {
		this.getTemplateConfigs().add(templateConfig);
	}

	/**
	 * @return the templateConfigs
	 */
	private Set<TemplateConfig> getTemplateConfigs() {
		if (this.templateConfigs == null) {
			this.templateConfigs = new HashSet<TemplateConfig>();
		}
		return this.templateConfigs;
	}

	/**
	 * @return the configs
	 */
	public String[] getConfigs() {
		return configs;
	}

	/**
	 * @param configs
	 *            the configs to set
	 */
	public void setConfigs(String[] configs) {
		this.configs = configs;
	}


}
